package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.ChinaCityAdapter;
import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.ListToStringUtil;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textillexepres.db.DBManager;

/**
 * 城市列表
 * 
 * @author sy
 * 
 */
public class ActivityChinaCity extends BaseActivity implements OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	private DBManager dbManager;
	public String city;
	int type = 1;// 1、直辖市 2、城市
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	String countryId;
	String province;
	CityBeans cityModel;
	String provinceID;
	private ListToStringUtil stringUtil;
	private ImageView img_other;
	private TextView title;
String countryNmae;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		province = getIntent().getStringExtra("province");
		country_lv_list = (ListView) findViewById(R.id.country_lv_list);
		provinceID = getIntent().getStringExtra("chinaID");
		countryNmae=getIntent().getStringExtra("countryNmae");
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		cityModel = new CityBeans();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);

		// database.close();

		handler = new Handler();

		mCityNames = getCityNames();
		
		setAdapter(mCityNames);
		img_other.setVisibility(View.GONE);
		title.setText("我的地址");
		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
	}

	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_city where father=" + provinceID
						+ " ORDER BY city", null);
		System.out.println("cursor" + cursor);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setProvince(cursor.getString(cursor
					.getColumnIndex("city")));

			cityModel.setId(cursor.getString(cursor.getColumnIndex("cityID")));
			names.add(cityModel);

		}
		return names;
	}

	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			// if (pos<= 0) {
			// return;
			// }
			searchType = true;
			cityModel = (CityBeans) country_lv_list.getAdapter().getItem(pos);
			city = cityModel.getProvince();
			cityModel.getId();

			String CityId = cityModel.getId();
			getPrivateNames(CityId);
			
			System.out.println("citycity" + cityModel.getProvince());
			finish();
			// country_lv_list.setSelection(0);// 设置选择项为第一项
		}

	}

	@SuppressWarnings({ "unused", "static-access" })
	private String getPrivateNames(String codeId) {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();

		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_area where father=" + codeId
						+ " ORDER BY area", null);
		System.out.println("cursor" + cursor);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);

			cityModel.setProvince(cursor.getString(cursor
					.getColumnIndex("area")));

			cityModel.setId(cursor.getString(cursor.getColumnIndex("father")));
			names.add(cityModel);

		}

		if (names == null || names.size() <= 0) {
			String name = cityModel.getProvince();
			SPUtils.put(ActivityChinaCity.this, null,
					SpPublicParams.AddressBeans,province+ name + "");
			
			
			
			SPUtils.put(ActivityChinaCity.this, null,
					SpPublicParams.COUTRYID,provinceID+"");
			SPUtils.put(ActivityChinaCity.this, null,
					SpPublicParams.PROVINCECITYID,cityModel.getId()+"");
			
			
			
			SPUtils.put(ActivityChinaCity.this, null,
					SpPublicParams.COUNTRY,countryNmae+"");
			SPUtils.put(ActivityChinaCity.this, null,
					SpPublicParams.PROVINCECITY,city+"");
			SPUtils.put(ActivityChinaCity.this, null,
					SpPublicParams.PROVINCE,province+"");
			
			Intent intent2 = new Intent("AddressBeans");
			sendBroadcast(intent2);
		} else {
			Intent intent = new Intent(ActivityChinaCity.this,
					ActivityAreaChina.class);
			intent.putExtra("id", cityModel.getId());
			intent.putExtra("province", province + city);
			intent.putExtra("countryNmae", countryNmae);
			intent.putExtra("provinceNmae", province);
			intent.putExtra("cityName",city);
			intent.putExtra("provinceID",provinceID);
			
			startActivity(intent);
			
		}
		return stringUtil.ListToString(names);
	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new ChinaCityAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		} else {
			finish();
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

}